package com.fare.taluo.action.plane.income;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import com.fare.taluo.application.plane.income.TaskPlfIncomeApplication;
import com.fare.taluo.base.struts2.json.BaseJsonStrutsAction;
import com.fare.taluo.base.struts2.json.JsonReturnBean;
import com.fare.taluo.conf.system.DefaultPropertiesLoader;
import com.fare.taluo.core.domain.config.line.OccupRateEntity;
import com.fare.taluo.io.excel.ExcelImportException;
import com.fare.taluo.vo.income.plfIncomeVO;



@Namespace("/plane/income/plfimport")
public class PlfImportAction extends BaseJsonStrutsAction{
	
	@Inject
	private TaskPlfIncomeApplication taskPlfIncomeApplication;
	
	private static final long serialVersionUID = -1814256390260877202L;
		
	private String excelFileName;
	
	private File uploadFile;
	
	private InputStream excelFileStream;

	public String getExcelFileName() {
		return excelFileName;
	}

	public void setExcelFileName(String excelFileName) {
		this.excelFileName = excelFileName;
	}

	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public InputStream getExcelFileStream() {
		return excelFileStream;
	}

	public void setExcelFileStream(InputStream excelFileStream) {
		this.excelFileStream = excelFileStream;
	}
	
	@Action(value="dataTemplate",results={@Result(name="success", 
			type="stream",params={"contentType","application/vnd.ms-excel",
			"contentCharSet", "UTF-8",
			"inputName","excelFileStream",
			"contentDisposition","attachment;filename=${excelFileName}.xls",
			"bufferSize","1024" })})
	public String dataTemplate() throws Exception{
		excelFileName = java.net.URLEncoder.encode("PlfIncomeMasterplate", "UTF-8");
		File downFile = new File(DefaultPropertiesLoader.getAppExcelFilePath()+"/PLF_INCOME_IMPORT_TEMPLATE.xls");
		excelFileStream = new FileInputStream(downFile);
		
		return SUCCESS;
	}

	@Action(value="excelImport",interceptorRefs = @InterceptorRef(value = "fileUploadStack"),
			results={@Result(name="success", type="json", params={"root","returnBean"})})
	public String excelImport() throws Exception{
//		List<OccupRateEntity> Incomes=null;
		List<plfIncomeVO> incomes=null;
//		List<OccupRateEntity> incomes = new ArrayList<OccupRateEntity>();
//		List<CrawlLineVO> lines = null;
		try{
			returnBean = new JsonReturnBean();
			incomes = (List<plfIncomeVO>)new PlfIncomeExcelImport().readExcel(uploadFile);
			
//			if(Incomes !=null){
//				for(OccupRateEntity e : Incomes){
//					e.saveOrUpdateOccupRateEntity(e);								
//				}
//			}
			
			if(incomes != null) 
				taskPlfIncomeApplication.importTasks(incomes);
			returnBean.setHasOk(true);
		}catch(ExcelImportException e){
			e.printStackTrace();
			logger.error(e);
			returnBean.setHasOk(false);
			returnBean.setTip(JsonReturnBean.TIP_FAIL);
			returnBean.setBean(e.getMessage());
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e);
			returnBean.setHasOk(false);
			returnBean.setTip(JsonReturnBean.TIP_ERROR);
			returnBean.setBean(e.getMessage());
		}finally{
			
		}
		return SUCCESS;
	}
	
}
